﻿/*
***********************************************************************
Software License Agreement

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
***********************************************************************

Author:
Hisham Kedjour (hkedjour@gmail.com)
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using Multitouch.Driver.Logic;

namespace JooJooTouchDriver
{
	public class TouchHandlerForm : Form
	{
		public IntPtr hDevice;

		DriverCommunicator driverCommunicator = new DriverCommunicator();

		private Point lastPoint = new Point(-1, -1);
		private bool wasDown;

		private int[] id = {0,1};
		private bool inRightClick;
		private Timer rightClickTimer;

		private int allowRightClick;


		short LoWord(uint l)
		{
			return ((short) (l & 0xffff));
		}

		short HiWord(uint l)
		{
			return ((short) ((l >> 16) & 0xffff));
		}

		public TouchHandlerForm()
		{
			Shown += delegate { Hide(); };
		}

		protected override void WndProc(ref Message m)
		{
			if (m.Msg != 0x464)
			{
				base.WndProc(ref m);
				return;
			}

			int iCx = LoWord((uint) m.LParam.ToInt32());
			int iCy = HiWord((uint) m.LParam.ToInt32());

			ushort x = (ushort) (iCx*HidContactInfo.MaxSize/2048);
			ushort y = (ushort) (iCy*HidContactInfo.MaxSize/2048);

			if (AppSettings.InvertX) x = (ushort) (HidContactInfo.MaxSize - x);
			if (AppSettings.InvertY) y = (ushort) (HidContactInfo.MaxSize - y);

			if (AppSettings.Swap)
			{
				var t = x;
				x = y;
				y = t;
			}
			
			var newPoint = new Point(x, y);

			int finger = ((m.WParam.ToInt32() >> 24) & 0x3F) - 1;

			bool isDown =(m.WParam.ToInt32() & 0x01) != 0;

			HidContactState state;

			int thisId = id[finger];

			HidContactInfo cnt = new HidContactInfo
			                     	{
			                     		Pressure = 100,
										Height = 10,
										Width = 10,
										Timestamp = DateTime.Now,
										Id = (ushort) thisId,
										X = x,
										Y = y,
			                     	};

			if (finger == 1)
			{
				inRightClick = false;
				allowRightClick = 2;
			}

			if (!isDown)
			{
				if (allowRightClick > 0) allowRightClick--;
				state = HidContactState.Removed;
				id[finger] = unchecked(id[finger] + 2);
			}
			else if (allowRightClick == 0 && lastPoint == newPoint && wasDown)
			{
				try		// Statistically this block caused most bugs, I wrap it to recover from undiscovered problems!!!
				{
					// => Touch, Hold, then remove
					rightClickTimer = new Timer();
					rightClickTimer.Tick += delegate
								{
									if (inRightClick)
									{
										cnt.State = HidContactState.Removed;
										driverCommunicator.Enqueue(cnt);
										id[finger] = unchecked(id[finger] + 2);
									}

									inRightClick = false;
									rightClickTimer.Enabled = false;
									rightClickTimer = null;
								};
					rightClickTimer.Interval = 380;
					rightClickTimer.Enabled = true;
					inRightClick = true;
					lastPoint = newPoint;
					wasDown = true;
				}
				catch
				{
					inRightClick = false;
					rightClickTimer = null;
				}

				return;
			}
			else
			{
				inRightClick = false;
				state = HidContactState.Updated;
			}

			lastPoint = newPoint;
			wasDown = isDown;
			cnt.State = state;
			
			driverCommunicator.Enqueue(cnt);
		}
	}
}
